import 'package:erp_widget/src/utils/index.dart';
import 'package:erp_widget/src/utils/screen/index.dart';
import 'package:flutter/material.dart';

import '../../theme/style.dart';

/// 文字按钮
/// github地址:https://github.com/nivisi/cupertino_text_button
class DxTextButton extends StatefulWidget {
  /// 是否禁用
  final bool disabled;

  /// 外边距
  final EdgeInsets margin;

  /// 内边距
  final EdgeInsets padding;

  /// 按钮文本
  final String title;
  final Color? color;
  final double? size;

  /// 按钮文本样式
  final TextStyle? titleStyle;

  /// 点击事件
  final VoidCallback? onClick;

  const DxTextButton({
    Key? key,
    this.disabled = false,
    this.margin = EdgeInsets.zero,
    this.padding = EdgeInsets.zero,
    this.title = '',
    this.color,
    this.size,
    this.titleStyle,
    this.onClick,
  }) : super(key: key);

  @override
  State<DxTextButton> createState() => _DxTextButtonState();
}

class _DxTextButtonState extends State<DxTextButton> with SingleTickerProviderStateMixin {
  late final AnimationController _animationController;
  late final ColorTween _colorTween;
  late final Animation<Color?> _animation;

  final Curve _curve = Curves.fastOutSlowIn;
  final Duration _forwardDuration = const Duration(milliseconds: 20);
  final Duration _backwardDuration = const Duration(milliseconds: 200);

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(vsync: this);
    _colorTween = ColorTween();
    _animation = _colorTween.animate(_animationController);
  }

  TextStyle _buildTextStyle() {
    return widget.titleStyle ?? TextStyle(color: widget.color ?? DxStyle.$4E89F7, fontSize: widget.size ?? 12.sp);
  }

  Color _buildColor() {
    return _buildTextStyle().color ?? DxStyle.$4E89F7;
  }

  @override
  void didUpdateWidget(covariant DxTextButton oldWidget) {
    super.didUpdateWidget(oldWidget);

    if (widget.titleStyle != oldWidget.titleStyle || widget.color != oldWidget.color || widget.size != oldWidget.size) {
      _colorTween.begin = _buildColor();
      _colorTween.end = _buildColor().withOpacity(0.6);
    }

    setState(() {});
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _colorTween.begin = _buildColor();
    _colorTween.end = _buildColor().withOpacity(0.6);
  }

  @override
  Widget build(BuildContext context) {
    Widget $widget = GestureDetector(
      onTapDown: (_) {
        if (widget.disabled) return DxTools.noVip(context);
        _animationController.animateTo(1.0, curve: _curve, duration: _forwardDuration);
      },
      onTapUp: (_) {
        if (widget.disabled) return DxTools.noVip(context);
        _animationController.animateTo(0.0, curve: _curve, duration: _backwardDuration);
      },
      onTap: () {
        if (widget.disabled) return DxTools.noVip(context);
        widget.onClick?.call();
      },
      child: Container(
        margin: widget.margin,
        padding: widget.padding,
        color: Colors.transparent,
        child: AnimatedBuilder(
          animation: _animationController,
          builder: (_, __) => Text(
            widget.title,
            style: _buildTextStyle().copyWith(color: _animation.value),
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
    );
    return widget.disabled == true ? Opacity(opacity: DxStyle.disabledOpacity, child: $widget) : $widget;
  }
}
